package com.taptech.doufu.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.weex.image.TFImageViewUtil;
import com.taptech.doufu.ui.fragment.CommonCpFragment;
import com.taptech.doufu.ui.dialog.CpAddContentDialog;
import com.taptech.doufu.ui.fragment.NewMyCpFragment;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.adapter.ScrollAbleViewPagerAdapter;
import com.taptech.doufu.bean.MineContentBean;
import com.taptech.doufu.bean.UserWorksBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.PersonalBaseAccount;
import com.taptech.doufu.ui.view.PersonalTabViewGroup;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.ui.view.ScrollableLayout;
import com.taptech.doufu.ui.fragment.ScrollAbleFragment;
import com.taptech.doufu.util.DisplayUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.WaitDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.ViewPager;

public class PersonalActivity extends DiaobaoBaseActivity implements OnClickListener, HttpResponseListener
{

    private ImageView mPersonalHeadImageView;
    // 个人信息关注/粉丝/动态/签到组件
    private LinearLayout mFollowButton, mFansButton, mMessageButton, mDouziButton;
    // 文字和图像控件
    private TextView mPersonalFollowCount;
    private TextView mPersonalFansCount;
    private TextView mPersonalActivityCount, user_signature;
    private TextView mDouziCount;
    private TextView mNickName;
    private ImageView mUserImg;
    private View personal_bac;
    private ScrollableLayout mScrollLayout;
    private WaitDialog dialog;
    private ImageView no_content_img;
    private ImageView add_content_iv;

    private TextView tvLevel,tvVip;
    private ImageView ivLevel,ivVip;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_personal_activity);
        initHeadView();
        loadData();
    }

    private void loadData()
    {
        dialog = new WaitDialog(this, R.style.updateDialog);
        dialog.show();
        if (AccountService.getInstance().getUserUid() != null)
        {
            String userId = AccountService.getInstance().getBaseAccount().getUid();
            PersonalInfoService.getInstance().getUserWorksRequest(this, userId);
            PersonalInfoService.loadPersonalCenterContent(PersonalActivity.this, userId);
        }
    }

    private void initHeadView()
    {

        tvLevel = (TextView) findViewById(R.id.tvLevel);
        tvVip = (TextView) findViewById(R.id.tvVip);
        ivLevel = (ImageView) findViewById(R.id.ivLevel);
        ivVip = (ImageView) findViewById(R.id.ivVip);
        add_content_iv = (ImageView) findViewById(R.id.add_content_iv);
        add_content_iv.setOnClickListener(this);
        no_content_img = (ImageView) findViewById(R.id.no_content_img);
        mPersonalHeadImageView = (ImageView) findViewById(R.id.head_title_imageview);
        mNickName = (TextView) findViewById(R.id.nick_name);
        mUserImg = (ImageView) findViewById(R.id.personal_activity_other_dynamic_user_portrait);
        personal_bac = (View) findViewById(R.id.personal_bac);
        mFollowButton = (LinearLayout) findViewById(R.id.personal_follow_button);
        mFansButton = (LinearLayout) findViewById(R.id.personal_fans_button);
        mMessageButton = (LinearLayout) findViewById(R.id.personal_message_button);
        mDouziButton = (LinearLayout) findViewById(R.id.enter_douzi_button);
        mFollowButton.setOnClickListener(this);
        mFansButton.setOnClickListener(this);
        mMessageButton.setOnClickListener(this);
        mDouziButton.setOnClickListener(this);
        personal_bac.setOnClickListener(this);
        mUserImg.setOnClickListener(this);
        mPersonalFollowCount = (TextView) findViewById(R.id.personal_follow_count);
        mPersonalFansCount = (TextView) findViewById(R.id.personal_fans_count);
        mPersonalActivityCount = (TextView) findViewById(R.id.personal_activity_count);
        mDouziCount = (TextView) findViewById(R.id.douzi_count);
        user_signature = (TextView) findViewById(R.id.user_signature);

    }

    private PersonalTabViewGroup mTabView;
    private List<ScrollAbleFragment> mSweepListViews;
    private TTHomeViewPager mViewPager;
    private ScrollAbleViewPagerAdapter viewPagerAdapter;

    protected void initUpView(UserWorksBean userWorksBean)
    {
        if (userWorksBean == null)
        {
            return;
        }
        UserBean userBean = userWorksBean.getUser();
        if (userBean != null){
            if (userBean.getLevel() > 0) {
                tvLevel.setText("豆花 Lv."+userBean.getLevel());
                GlideUtil.displayImage(ivLevel,userBean.getLevel_icon());
                tvLevel.setVisibility(View.VISIBLE);
                ivLevel.setVisibility(View.VISIBLE);
            }else{
                tvLevel.setVisibility(View.GONE);
                ivLevel.setVisibility(View.GONE);
            }
            tvVip.setVisibility(View.VISIBLE);
            ivVip.setVisibility(View.VISIBLE);
            if (userBean.getIs_year_vip() > 0) {
                ivVip.setImageResource(R.drawable.icon_year_vip);
                tvVip.setText("年度会员");
            } else if (userBean.getIs_vip() > 0) {
                ivVip.setImageResource(R.drawable.icon_normal_vip);
                tvVip.setText("会员");
            }else{
                tvVip.setVisibility(View.GONE);
                ivVip.setVisibility(View.GONE);
            }
        }
        mScrollLayout = (ScrollableLayout) findViewById(R.id.scrollableLayout);
        mTabView = (PersonalTabViewGroup) findViewById(R.id.top_tab_view);
        mViewPager = (TTHomeViewPager) findViewById(R.id.sweep_view_pager);
        mViewPager.setOffscreenPageLimit(6);
        mSweepListViews = new ArrayList<ScrollAbleFragment>();
        mSweepListViews.add(CommonCpFragment.newInstance(CommonCpFragment.TYPE_PERSONAL_NOVEL, AccountService.getInstance().getUserUid()));
        mSweepListViews.add(CommonCpFragment.newInstance(CommonCpFragment.TYPE_PERSONAL_DRAW, AccountService.getInstance().getUserUid()));
        mSweepListViews.add(CommonCpFragment.newInstance(NewMyCpFragment.TYPE_PERSONAL_PUBLISHED_NOTE, AccountService.getInstance().getUserUid()));
        mSweepListViews.add(CommonCpFragment.newInstance(NewMyCpFragment.TYPE_PERSONAL_PUBLISHED_SWEEP, AccountService.getInstance().getUserUid()));
        mScrollLayout.getHelper().setCurrentScrollableContainer(mSweepListViews.get(0));
        List<String> name_lists = new ArrayList<String>();
        name_lists.add("小说");
        name_lists.add("绘画");
        name_lists.add("帖子");
        name_lists.add("文评");
//        if (Integer.parseInt(userWorksBean.getTopic_count()) > 0)
//        {
//            name_lists.add("话题");
//            mSweepListViews.add(CommonCpFragment.newInstance(NewMyCpFragment.TYPE_PERSONAL_PUBLISHED_UGC, AccountService.getInstance().getUserUid()));
//        }
//        if (Integer.parseInt(userWorksBean.getCosplay_count()) > 0)
//        {
//            name_lists.add("COS");
//            mSweepListViews.add(CommonCpFragment.newInstance(NewMyCpFragment.TYPE_PERSONAL_PUBLISHED_COS, AccountService.getInstance().getUserUid()));
//        }
        if (mSweepListViews.size() == 0)
        {
            no_content_img.setVisibility(View.VISIBLE);
            return;
        }
        String[] NAMESTRING = (String[]) name_lists.toArray(new String[name_lists.size()]);
        mTabView.putStringsTabs(NAMESTRING);
        mTabView.setChoosePageListener(new PersonalTabViewGroup.ChoosePage()
        {
            @Override
            public void updatePage(int position)
            {
                mViewPager.setCurrentItem(position, true);
            }
        });
        viewPagerAdapter = new ScrollAbleViewPagerAdapter(getSupportFragmentManager(), mSweepListViews);
        mViewPager.setAdapter(viewPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {

            @Override
            public void onPageSelected(int arg0)
            {
                // TODO Auto-generated method stub
                mTabView.moveTopStripTo(arg0);
                mScrollLayout.getHelper().setCurrentScrollableContainer(mSweepListViews.get(arg0));
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub
            }

        });

    }

    @Override
    protected void onResume()
    {
        setUserInfo();
        super.onResume();
    }

    public void setUserInfo()
    {
        PersonalBaseAccount account = AccountService.getInstance().getBaseAccount();
        // 用户头像
        if (AccountService.getInstance().getUserPortaritUrl() != null)
        {
            GlideUtil.displayCircleImageBorder(mUserImg, AccountService.getInstance().getUserPortaritUrl());
            TFImageViewUtil.showVagueImage(mPersonalHeadImageView,AccountService.getInstance().getUserPortaritUrl(),8);
        } else
        {
            GlideUtil.displayCircleImageBorder(mUserImg, account.getUser_head_img());
            TFImageViewUtil.showVagueImage(mPersonalHeadImageView,account.getUser_head_img(),8);

        }
        mPersonalFollowCount.setText(account.getAttentions_counts());
        mPersonalFansCount.setText(account.getFans_counts());
        mPersonalActivityCount.setText(account.getActivity_count());
        mDouziCount.setText(account.getUser_scores());
        mNickName.setText(account.getNickname());
        user_signature.setText(account.getUser_signature());
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.personal_follow_button:
            {
                Intent intent = new Intent(this, PersonalAttentionsActivity.class);
                intent.putExtra(Constant.UID, AccountService.getInstance().getUserUid());
                startActivity(intent);
                break;
            }

            case R.id.personal_fans_button:
            {
                Intent intent = new Intent(this, PersonalFansActivity.class);
                intent.putExtra(Constant.UID, AccountService.getInstance().getUserUid());
                startActivity(intent);
                break;
            }
            // 个人动态
            case R.id.personal_message_button:
            {
                Intent intent = new Intent(this, PersonalNewDynamicActivity.class);
                intent.putExtra(Constant.UID, AccountService.getInstance().getUserUid());
                startActivity(intent);
            }
            break;
            case R.id.enter_douzi_button:
            {

                Intent intent = new Intent(this, PersonalPointsActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.personal_activity_other_dynamic_user_portrait:
            {
                SimpleWeexActivity.startActivity(this,"vue/login/TFUserInfoEditPage.js");
                break;
            }
            case R.id.personal_bac:
                finish();
                break;
            case R.id.add_content_iv:
                DisplayUtil.makeAddContentPopupWindow(this, CpAddContentDialog.TYPE_CPDES);
                break;
            default:
                break;
        }

    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        dialog.cancel();
        if (response == null)
        {
            UIUtil.toastMessage(this, Constant.loadingFail);
            return;
        }
        if (response.getStatus() == 0)
        {
            switch (handleType)
            {
                // 获取个人中心的内容列表信息
                case PersonalInfoService.HANDLE_TYPE_LOAD_PERSONAL_CENTER_CONTENT:
                    final JSONObject json = (JSONObject) response.getData();
                    displayJsonData(json);
                    break;
                case PersonalInfoService.HANDLE_HAS_WORKS:
                    JSONObject data = (JSONObject) response.getData();
                    UserWorksBean userWorksBean = new UserWorksBean();
                    userWorksBean.setJson2(data);
                    initUpView(userWorksBean);
                    break;
            }

        } else
        {
            if (response.getUser_msg() != null && !response.getUser_msg().isEmpty())
            {
                UIUtil.toastMessage(this, response.getUser_msg());
            } else
            {
                UIUtil.toastMessage(this, Constant.loadingFail);
            }
        }
    }

    private void displayJsonData(JSONObject data)
    {
        if (data != null)
        {
            MineContentBean contentBean = new MineContentBean(PersonalActivity.this);
            contentBean.setJson2(data);
            if (contentBean.getUser() != null)
            {
                //PersonalBaseAccount account = AccountService.getInstance().getBaseAccount();
                AccountService.getInstance().getBaseAccount().setNovel_count(contentBean.getUser().getNovel_count());
                AccountService.getInstance().getBaseAccount().setJournal_count(contentBean.getUser().getJournal_count());
                AccountService.getInstance().getBaseAccount().setNovel_review_count(contentBean.getUser().getNovel_review_count());
                AccountService.getInstance().getBaseAccount().setTopic_count(contentBean.getUser().getTopic_count());
                AccountService.getInstance().getBaseAccount().setActivity_count(contentBean.getUser().getActivity_count());
                AccountService.getInstance().getBaseAccount().setUser_scores(contentBean.getUser().getUser_scores());
                AccountService.getInstance().getBaseAccount().setAttentions_counts(contentBean.getUser().getAttentions_counts());
                AccountService.getInstance().getBaseAccount().setFans_counts(contentBean.getUser().getFans_counts());
                AccountService.getInstance().getBaseAccount().setSignatured(contentBean.getUser().getSignatured());
                setUserInfo();
            }
        }
    }

    public static void startActivity(Context context,String uid) {
        Intent intent = new Intent(context, PersonalActivity.class);
        intent.putExtra(Constant.USER_ID, uid);
        context.startActivity(intent);
    }

}